package com.hkf.shop.controller;

import com.hkf.shop.enetity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @author huangkf
 * @version 1.0
 * @date 2022/12/16:11:36
 * @description 消费者控制类
 */


@RestController
@RequestMapping("userConsumer")
public class UserConsumerController {


    @Autowired
    private RestTemplate restTemplate;

   //private final static String LOCALHOST_PREFIX = "http://localhost:6005"; //shopProvider
   private final static String LOCALHOST_PREFIX = "http://SHOP-PROVIDER";


    @GetMapping("test")
    public String test() {

        return "  hello,resttemplate";
    }


    //获取所有用户
    @GetMapping("getUsers")
    public List<User> findAll() {

        List<User> forObject = restTemplate.getForObject(LOCALHOST_PREFIX + "/user/getAll", List.class);
        return forObject;
    }

    //根据id获取用户

    @GetMapping("byId/{id}")
    public User findByUser(@PathVariable(value = "id") String id) {
        User forObject = restTemplate.getForObject(LOCALHOST_PREFIX + "/user/getUserById/"+id, User.class);
        return forObject;
    }

    //添加用户
    @PostMapping("addUser")
    public boolean addUser(User user) {
        Boolean aBoolean = restTemplate.postForObject(LOCALHOST_PREFIX + "/addUser", user, Boolean.class);


        return aBoolean;
    }
}
